package at.fhooe.mcm12.java.serializer;

import java.io.File;
import java.io.FileWriter;

public class CSVWriter extends AWriter {
	public CSVWriter() {
		m_fileExt = ".csv";
		m_worker = new Thread(this);
		start();
	}

	@Override
	public void write(WriterQueueElement _elem) {

		String file = _elem.getFile() + m_fileExt;
		try {
			File f = new File(file);
			final File parent_directory = f.getParentFile();
			if (null != parent_directory) {
				// Create directory structure
				parent_directory.mkdirs();
			}
			boolean writeHeader = false;
			if (!f.exists()) {
				writeHeader = true;
			}
			au.com.bytecode.opencsv.CSVWriter writer = new au.com.bytecode.opencsv.CSVWriter(
					new FileWriter(file, true), ';');
			if (writeHeader)
				writer.writeNext(_elem.getTags());
			writer.writeNext(_elem.getValues());
			writer.flush();
			writer.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
